package cn.eyesea.cc.frame.base.impl.assembly;

import cn.eyesea.cc.config.GlobalConfig;
import cn.eyesea.cc.entity.FieldInfo;
import cn.eyesea.cc.frame.base.impl.resultresource.JavaClassResultResource;
import cn.eyesea.cc.frame.base.impl.resultresource.VariableResultResource;
import cn.eyesea.cc.frame.component.NameComponent;
import cn.eyesea.cc.frame.component.ResourceComponent;
import cn.eyesea.cc.frame.enums.ResultTypeEnum;
import com.google.common.collect.Lists;
import cn.eyesea.cc.frame.enums.LogicalLayerTypeEnum;
import cn.eyesea.cc.entity.DomainInfo;

import cn.eyesea.cc.frame.base.Assembly;
import cn.eyesea.cc.frame.base.ResultResource;
import cn.eyesea.cc.frame.context.AssemblyRealizeContext;
import cn.eyesea.cc.frame.enums.LogicalTypeEnum;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2021/7/26 4:23 下午
 */
public class DoAssembly extends Assembly {

    @Override
    public LogicalTypeEnum getLogicalType() {
        return LogicalTypeEnum.ENTITY;
    }

    @Override
    public List<LogicalTypeEnum> relyLogicalTypes() {
        return Arrays.asList(LogicalTypeEnum.VARIABLE);
    }

    @Override
    public ResultResource realize(AssemblyRealizeContext assemblyRealizeContext) {
        JavaClassResultResource doResultResource = new JavaClassResultResource();
        LogicalLayerTypeEnum logicalLayer = assemblyRealizeContext.getLogicalLayer();
        DomainInfo mainDomainInfo = assemblyRealizeContext.getMainDomainInfo();
        doResultResource.setName(NameComponent.createResultResourceName(mainDomainInfo, getLogicalType()));
        doResultResource.setResourceType(ResultTypeEnum.JAVA_CLASS);
        doResultResource.setPackageInfo(createPackage(logicalLayer, assemblyRealizeContext.getGlobalConfig()));
        doResultResource.setResourcePath(createResourcePath(doResultResource, assemblyRealizeContext.getGlobalConfig()));
        doResultResource.setResourceFunctions(Lists.newArrayList());
        doResultResource.setDomainInfo(mainDomainInfo);
        doResultResource.setLogicalTypeEnum(getLogicalType());
        doResultResource.setLogicalLayer(logicalLayer);
        List<FieldInfo> columnInfoList = mainDomainInfo.getColumns();
        for (FieldInfo fieldInfo : columnInfoList) {
            VariableResultResource convert = ResourceComponent.convert(fieldInfo);
            doResultResource.usage(convert);
        }
        doResultResource.generateCode(assemblyRealizeContext.getGlobalConfig());
        return doResultResource;
    }

    @Override
    public ResultResource relyRealize(AssemblyRealizeContext assemblyRealizeContext) {
        return null;
    }

}
